/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-5-12 下午5:56
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.recycleviewdemo.util;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;

/**
 * Created by dqhl on 17/5/12.
 */
public class CircleDrawable extends Drawable {

    //画笔
    private Paint paint;
    private int mWidth;
    private RectF rectF;
    private Bitmap mBitmap;

    public CircleDrawable(Bitmap bitmap) {
        this.mBitmap = bitmap;
        //平铺
        BitmapShader shader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
        paint = new Paint();
        //平滑
        paint.setAntiAlias(true);
        //设置着色器
        paint.setShader(shader);

        //获取半径
        mWidth = Math.min(mBitmap.getWidth(),mBitmap.getHeight());
    }

    @Override
    public void draw(Canvas canvas) {
//        canvas.drawCircle(mWidth/2,mWidth/2,mWidth/2,paint);
        canvas.drawRoundRect(rectF,30,30,paint);
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        rectF = new RectF(left,top,right,bottom);
    }

    @Override
    public void setAlpha(int alpha) {
        //设置画笔的透明度
        paint.setAlpha(alpha);

    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
        paint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        //支持窗口的透明度
        return PixelFormat.TRANSLUCENT;
    }

        /**
         *
         * 返回drawable的实际的宽高
         *
         * */
    @Override
    public int getIntrinsicHeight() {
        return mBitmap.getHeight();
    }

    @Override
    public int getIntrinsicWidth() {
        return mBitmap.getWidth();
    }
}
